package com.kfm.base.function2;

import java.util.Scanner;


public class Demo02 {

    public static void main(String[] args) {
        /*
        从键盘输入一个数字，设计一个方法通过二分查找返回其位置（不存在返回 -1）。
         */
        int[] nums = {1, 2, 3, 4, 5, 6, 7};
        int n = input();
        int index = binarySearch(nums, n);
        System.out.println(index);
    }

    public static int binarySearch(int[] nums, int n) {
        int left = 0;
        int right = nums.length - 1;

        while(left <= right){
            int middle = (left + right) / 2;
            if (nums[middle] == n){
                return middle;
            } else if (nums[middle] < n){
                left = middle + 1;
            } else {
                right = middle - 1;
            }
        }
        return -1;
    }

    public static int input(){
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        return num;
    }
}
